//
//  AppDelegate.swift
//  新浪微博
//
//  Created by yaozhen on 2017/11/27.
//  Copyright © 2017年 ZYZ. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setUpAdditions()
        window = UIWindow()
        window?.backgroundColor = UIColor.red
        window?.rootViewController = MineViewController()
        window?.makeKeyAndVisible()
        loadAppInfo()
        return true
    }
}
//设置应用程序额外信息
extension AppDelegate{
    private func setUpAdditions(){
        
        //1 设置 SVProgressHUD 最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        //2 设置网络加载指示器 导航栏菊花
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //3设置用户授权显示通知
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: UNAuthorizationOptions([.alert,.badge,.carPlay,.sound]), completionHandler: { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            })
        } else {
            //取得用户授权显示显示通知[上方的提示条/声音/badgeNumber]
         UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings.init(types: [.alert,.badge,.sound] , categories: nil))
        }
    }
}
extension AppDelegate {
    private func loadAppInfo() {
        DispatchQueue.global().async {
            //1  url
            let url = Bundle.main.url(forResource: "demo", withExtension: "json")
            //2 data
            let data = NSData.init(contentsOf: url!)
            //3 写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("demo.json")
            //保存在沙河路径 等待程序下一次启动使用！
            data?.write(toFile: jsonPath, atomically: true)
            print("应用程序加载完毕\(jsonPath)")
        }
    }
}

